<?php
class FieldSet extends DirectionalContainer implements LabelledElement {
	
	protected $inlineLabel;
	protected $labelVisibility;
	
	private $label;
	
	public function __construct($label, $elementId = NULL) {
		
		parent::__construct($elementId);
		
		$this->label = $label;
	}
	
	public function setLabelVisibility($labelVisibility) {
	
		$this->labelVisibility = $labelVisibility;
		
		foreach ($this->elementArray as $element) {
			
			if ($element instanceof LabelledElement) {
				$element->setLabelVisibility($labelVisibility);
			}
		}
	}
	
	public function setInlineLabel($inlineLabel) {
	
		$this->inlineLabel = $inlineLabel;
	}
	
	public function getLabelVisibility() {
		
		return $this->labelVisibility;
	}
	
	public function getInlineLabel() {
		
		return $this->inlineLabel;
	}
	
	protected function renderOpeningMarkup() {
		
		$direction = '';
		if ($this->groupDirection == Direction::$groupHorizontal) {
			$direction = 'data-type="horizontal"';
		}
		
		echo '<fieldset data-role="controlgroup" ', $direction, ' ', $this->formatAttributes(), ' >';
		
		if ($this->labelVisibility == Label::$labelVisible) {
			echo '<legend>', $this->label, '</legend>';
		}
	}
	
	protected function renderElement() {
	
		$this->renderOpeningMarkup();
	
		foreach ($this->elementArray as $element) {
			$element->render();
		}
	
		$this->renderClosingMarkup();
	}
	
	protected function renderClosingMarkup() {
		
		echo '</fieldset>';
	}
}
?>